﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.display.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;

    public class Container extends Component {

        private var focusTraversalPolicy:FocusTraversalPolicy;
        protected var children:Array;
        protected var layout:LayoutManager;

        public function Container(){
            setName("Container");
            this.focusTraversalPolicy = null;
            this.children = new Array();
            this.layout = new EmptyLayout();
        }
        public function setLayout(_arg1:LayoutManager):void{
            this.layout = _arg1;
            revalidate();
        }
        public function getLayout():LayoutManager{
            return (this.layout);
        }
        public function setFocusTraversalPolicy(_arg1:FocusTraversalPolicy):void{
            this.focusTraversalPolicy = _arg1;
        }
        public function getFocusTraversalPolicy():FocusTraversalPolicy{
            var _local1:FocusTraversalPolicy;
            var _local2:FocusManager;
            if (this.focusTraversalPolicy == null){
                _local1 = null;
                if (getParent() != null){
                    _local1 = getParent().getFocusTraversalPolicy();
                };
                if (_local1 == null){
                    _local2 = FocusManager.getManager(stage);
                    if (_local2 != null){
                        _local1 = _local2.getDefaultFocusTraversalPolicy();
                    };
                    if (_local1 == null){
                        _local1 = new ContainerOrderFocusTraversalPolicy();
                    };
                };
                return (_local1);
            };
            return (this.focusTraversalPolicy);
        }
        override public function invalidate():void{
            this.layout.invalidateLayout(this);
            super.invalidate();
        }
        override public function validate():void{
            var _local1:int;
            if (!valid){
                this.doLayout();
                _local1 = 0;
                while (_local1 < this.children.length) {
                    this.children[_local1].validate();
                    _local1++;
                };
                valid = true;
            };
        }
        public function doLayout():void{
            if (isVisible()){
                this.layout.layoutContainer(this);
            };
        }
        public function reAppendChildren():void{
            var _local1:Array = this.children.concat();
            this.removeAll();
            var _local2:int;
            while (_local2 < _local1.length) {
                this.append(_local1[_local2]);
                _local2++;
            };
            revalidate();
        }
        public function append(_arg1:Component, _arg2:Object=null):void{
            this.insertImp(-1, _arg1, _arg2);
        }
        public function appendAll(... _args):void{
            var _local2:*;
            var _local3:Component;
            for each (_local2 in _args) {
                _local3 = (_local2 as Component);
                if (_local3 != null){
                    this.append(_local3);
                };
            };
        }
        public function insert(_arg1:int, _arg2:Component, _arg3:Object=null):void{
            this.insertImp(_arg1, _arg2, _arg3);
        }
        public function insertAll(_arg1:int, ... _args):void{
            var _local3:*;
            var _local4:Component;
            for each (_local3 in _args) {
                _local4 = (_local3 as Component);
                if (_local4 != null){
                    this.insert(_arg1, _local4);
                    _arg1++;
                };
            };
        }
        protected function insertImp(_arg1:int, _arg2:Component, _arg3:Object=null):void{
            var _local4:Container;
            if (_arg1 > this.getComponentCount()){
                throw (new RangeError("illegal component position when insert comp to container"));
            };
            if ((_arg2 is Container)){
                _local4 = this;
                while (_local4 != null) {
                    if (_local4 == _arg2){
                        throw (new ArgumentError("adding container's parent to itself"));
                    };
                    _local4 = _local4.getParent();
                };
            };
            if (_arg2.getParent() != null){
                _arg2.removeFromContainer();
            };
            if (_arg1 < 0){
                this.children.push(_arg2);
                addChild(_arg2);
            } else {
                addChildAt(_arg2, this.getChildIndexWithComponentIndex(_arg1));
                this.children.splice(_arg1, 0, _arg2);
            };
            _arg2.container = this;
            this.layout.addLayoutComponent(_arg2, ((_arg3)==null) ? _arg2.getConstraints() : _arg3);
            dispatchEvent(new ContainerEvent(ContainerEvent.COM_ADDED, this, _arg2));
            if (valid){
                revalidate();
            } else {
                invalidatePreferSizeCaches();
            };
        }
        override public function removeChild(_arg1:DisplayObject):DisplayObject{
            this.checkChildRemoval(_arg1);
            return (super.removeChild(_arg1));
        }
        override public function removeChildAt(_arg1:int):DisplayObject{
            this.checkChildRemoval(getChildAt(_arg1));
            return (super.removeChildAt(_arg1));
        }
        private function checkChildRemoval(_arg1:DisplayObject):void{
            var _local2:Component;
            if ((_arg1 is Component)){
                _local2 = (_arg1 as Component);
                if (_local2.getParent() != null){
                    throw (new ArgumentError("You should call remove method to remove a component child!"));
                };
            };
        }
        public function remove(_arg1:Component):Component{
            var _local2:int = this.getIndex(_arg1);
            if (_local2 >= 0){
                return (this.removeAt(_local2));
            };
            return (null);
        }
        public function removeAt(_arg1:int):Component{
            return (this.removeAtImp(_arg1));
        }
        protected function removeAtImp(_arg1:int):Component{
            if (_arg1 < 0){
                return (null);
            };
            var _local2:Component = this.children[_arg1];
            if (_local2 != null){
                this.children.splice(_arg1, 1);
                super.removeChild(_local2);
                _local2.container = null;
                this.layout.removeLayoutComponent(_local2);
                dispatchEvent(new ContainerEvent(ContainerEvent.COM_REMOVED, this, _local2));
                if (valid){
                    revalidate();
                } else {
                    invalidatePreferSizeCaches();
                };
            };
            return (_local2);
        }
        public function removeAll():void{
            while (this.children.length > 0) {
                this.removeAt((this.children.length - 1));
            };
        }
        public function getComponent(_arg1:int):Component{
            if ((((_arg1 < 0)) || ((_arg1 >= this.children.length)))){
                throw (new RangeError("Index out of container children bounds!!!"));
            };
            return (this.children[_arg1]);
        }
        public function getIndex(_arg1:Component):int{
            var _local2:int = this.children.length;
            var _local3:int;
            while (_local3 < _local2) {
                if (_arg1 == this.children[_local3]){
                    return (_local3);
                };
                _local3++;
            };
            return (-1);
        }
        public function getComponentCount():int{
            return (this.children.length);
        }
        public function isAncestorOf(_arg1:Component):Boolean{
            var _local2:Container = _arg1.getParent();
            if ((((_arg1 == null)) || ((_local2 == null)))){
                return (false);
            };
            while (_local2 != null) {
                if (_local2 == this){
                    return (true);
                };
                _local2 = _local2.getParent();
            };
            return (false);
        }
        protected function getChildIndexWithComponentIndex(_arg1:int):int{
            var _local2:int = this.getComponentCount();
            if ((((_arg1 < 0)) || ((_arg1 > _local2)))){
                throw (new RangeError("Out of index counting bounds, it should be >=0 and <= component count!"));
            };
            if (_arg1 == _local2){
                return (getHighestIndexUnderForeground());
            };
            return (getChildIndex(this.getComponent(_arg1)));
        }
        protected function getComponentIndexWithChildIndex(_arg1:int):int{
            var _local3:int;
            var _local4:int;
            var _local2:int = numChildren;
            if ((((_arg1 < 0)) || ((_arg1 > _local2)))){
                throw (new RangeError("Out of index counting bounds, it should be >=0 and <= numChildren!"));
            };
            if (_arg1 == _local2){
                return (this.getComponentCount());
            };
            _local3 = 0;
            _local4 = _arg1;
            while (_local4 < _local2) {
                if ((getChildAt(_local4) is Component)){
                    _local3++;
                };
                _local4++;
            };
            return ((this.getComponentCount() - _local3));
        }
        override protected function countMinimumSize():IntDimension{
            var _local1:IntDimension;
            if (ui != null){
                _local1 = ui.getMinimumSize(this);
            };
            if (_local1 == null){
                _local1 = this.layout.minimumLayoutSize(this);
            };
            if (_local1 == null){
                _local1 = super.countMinimumSize();
            };
            return (_local1);
        }
        override protected function countMaximumSize():IntDimension{
            var _local1:IntDimension;
            if (ui != null){
                _local1 = ui.getMaximumSize(this);
            };
            if (_local1 == null){
                _local1 = this.layout.maximumLayoutSize(this);
            };
            if (_local1 == null){
                _local1 = super.countMaximumSize();
            };
            return (_local1);
        }
        override protected function countPreferredSize():IntDimension{
            var _local1:IntDimension;
            if (ui != null){
                _local1 = ui.getPreferredSize(this);
            };
            if (_local1 == null){
                _local1 = this.layout.preferredLayoutSize(this);
            };
            if (_local1 == null){
                _local1 = super.countPreferredSize();
            };
            return (_local1);
        }

    }
}//package org.aswing 
